package com.cloud.adrian.novel.user.controller.front;

import com.cloud.adrian.novel.book.dto.req.BookCommentReqDto;
import com.cloud.adrian.novel.book.dto.resp.BookCommentRespDto;
import com.cloud.adrian.novel.common.auth.UserHolder;
import com.cloud.adrian.novel.common.constant.ApiRouterConstants;
import com.cloud.adrian.novel.common.constant.SystemConfigConstants;
import com.cloud.adrian.novel.common.req.PageReqDto;
import com.cloud.adrian.novel.common.resp.PageRespDto;
import com.cloud.adrian.novel.common.resp.RestResp;
import com.cloud.adrian.novel.user.dao.entity.UserInfo;
import com.cloud.adrian.novel.user.dto.req.UserInfoUpdateReqDto;
import com.cloud.adrian.novel.user.dto.req.UserLoginReqDto;
import com.cloud.adrian.novel.user.dto.req.UserRegisterReqDto;
import com.cloud.adrian.novel.user.dto.resp.UserCommentRespDto;
import com.cloud.adrian.novel.user.dto.resp.UserInfoRespDto;
import com.cloud.adrian.novel.user.dto.resp.UserLoginRespDto;
import com.cloud.adrian.novel.user.dto.resp.UserRegisterRespDto;
import com.cloud.adrian.novel.user.manager.feign.BookFeignManager;
import com.cloud.adrian.novel.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 前台门户-用户模块 控制层
 * @Author: xiangguiliang
 * @version: 1.0
 * @since: 2024/05/17
 */
@Tag(name = "UserController", description = "前台门户-用户模块")
@SecurityRequirement(name = SystemConfigConstants.HTTP_AUTH_HEADER_NAME)
@RestController
@RequestMapping(ApiRouterConstants.API_FRONT_USER_URL_PREFIX)
@RequiredArgsConstructor
public class FrontUserController {

    private final UserService userService;

    private final BookFeignManager bookFeignManager;

    /**
     * 用户注册接口
     */
    @Operation(summary = "用户注册接口")
    @PostMapping("register")
    public RestResp<UserRegisterRespDto> register(@Valid @RequestBody UserRegisterReqDto dto) {
        return userService.register(dto);
    }

    /**
     * 用户登录接口
     */
    @Operation(summary = "用户登录接口")
    @PostMapping("login")
    public RestResp<UserLoginRespDto> login(@Valid @RequestBody UserLoginReqDto dto) {
        return userService.login(dto);
    }

    /**
     * 发表评论接口
     */
    @Operation(description = "发表评论接口")
    @PostMapping("comment")
    public RestResp<Void> comment(@Valid @RequestBody BookCommentReqDto dto) {
        return bookFeignManager.publishComment(dto);
    }

    /**
     * 修改评论
     */
    @Operation(description = "修改评论")
    @PutMapping("comment/{id}")
    public RestResp<Void> updateComment(
            @Parameter(description = "评论ID") @PathVariable("id") Long id, String content) {

        return bookFeignManager.updateComment(id, content);
    }

    /**
     * 删除评论
     */
    @Operation(description = "删除评论")
    @DeleteMapping("comment/{id}")
    public RestResp<Void> deleteComment(@Parameter(description = "评论ID") @PathVariable Long id) {
        return bookFeignManager.deleteComment(id);
    }

    /**
     * 用户信息查询
     */
    @Operation(description = "用户信息查询")
    @GetMapping
    public RestResp<UserInfoRespDto> getUserInfo() {
        return userService.getUserInfo(UserHolder.getUserId());
    }

    /**
     * 用户信息修改
     */
    @Operation(description = "用户信息修改")
    @PutMapping
    public RestResp<Void> updateUserInfo(@Valid @RequestBody UserInfo userInfo) {
        return userService.updateUserInfo(userInfo);
    }

    /**
     * 用户评论查询
     */
    @Operation(description = "用户评论查询")
    @GetMapping("comments")
    public RestResp<PageRespDto<UserCommentRespDto>> getUserComments(PageReqDto dto) {
        return userService.getUserComments(UserHolder.getUserId(), dto);
    }
}
